﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FacialClub.Helpers;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
using FacialClub.Models.Repositories;
using FacialClub.Models.ViewModels;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// Customer Type Controller
    /// </summary>
    [Authorize]
    public class CustomerTypeController : Controller
    {
        #region Fields

        private const int DEFAULTPAGESIZE = 10;
        private readonly ICustomerTypeRepository _customerTypeRepository;

        #endregion

        #region Constructors

        /// <summary>
        /// Dependency Injection enabled constructors
        /// </summary>
        public CustomerTypeController() : this(new CustomerTypeRepository())
        {
        }

        /// <summary>
        /// Customer Type Controller Constructor
        /// </summary>
        /// <param name="customerTypeRepository"></param>
        public CustomerTypeController(ICustomerTypeRepository customerTypeRepository)
        {
            _customerTypeRepository = customerTypeRepository;
        }

        #endregion

        #region Actions

        /// <summary>
        /// Display the listing of Customer Type
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string strQuery, int? page)
        {
            IQueryable<CustomerType> customerType = !string.IsNullOrWhiteSpace(strQuery)
                                                        ? _customerTypeRepository.FindCustomerTypeByName(strQuery).
                                                              OrderBy(d => d.CreatedDate)
                                                        : _customerTypeRepository.FindAllCustomerType().OrderBy(
                                                            d => d.CreatedDate);
            PaginatedListCollection<CustomerType> paginatedCustomerType =
                customerType.ToPagedList(page.HasValue ? page.Value - 1 : 0, DEFAULTPAGESIZE);
            return View(paginatedCustomerType);
        }

        /// <summary>
        /// Details of Customer Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int? id)
        {
            if (id != null)
            {
                CustomerType customerType = _customerTypeRepository.GetCustomerTypeById(id.Value);

                if (customerType == null)
                {
                    return new FileNotFoundResult {Message = "No CustomerType found for that id"};
                }

                return View(new CustomerTypeViewModel(customerType));
            }

            return new FileNotFoundResult {Message = "No CustomerType found due to invalid CustomerType id"};
        }

        /// <summary>
        /// Create Customer Type
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Create()
        {
            var customerType = new CustomerType();
            return View(new CustomerTypeViewModel(customerType));
        }

        /// <summary>
        /// Create Customer Type
        /// </summary>
        /// <param name="customerType"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(CustomerType customerType)
        {
            if (ModelState.IsValid)
            {
                customerType.UserCreated = HttpContext.User.Identity.Name;
                customerType.CreatedDate = DateTime.Now.ToSqlDateTime();
                customerType.IsActive = Convert.ToBoolean(Request["IsActive"]);
                _customerTypeRepository.Add(customerType);
                _customerTypeRepository.Save();
                return RedirectToAction("Details", new {id = customerType.CustomerTypeId});
            }

            return View(new CustomerTypeViewModel(customerType));
        }

        /// <summary>
        /// Edit Customer Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            CustomerType customerType = _customerTypeRepository.GetCustomerTypeById(id);
            return View(new CustomerTypeViewModel(customerType));
        }

        /// <summary>
        /// Edit Customer Type
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            CustomerType customerType = _customerTypeRepository.GetCustomerTypeById(id);
            if (ModelState.IsValid)
            {
                try
                {
                    customerType.UserModified = HttpContext.User.Identity.Name;
                    customerType.ModifiedDate = DateTime.Now;
                    UpdateModel(customerType, "CustomerType");
                    _customerTypeRepository.Save();
                    return RedirectToAction("Details", new {id = customerType.CustomerTypeId});
                }
                catch
                {
                    return View(new CustomerTypeViewModel(customerType));
                }
            }
            return View(new CustomerTypeViewModel(customerType));
        }

        /// <summary>
        /// Delete Customer Type
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            CustomerType customerType = _customerTypeRepository.GetCustomerTypeById(id);
            return customerType == null ? View("NotFound") : View(new CustomerTypeViewModel(customerType));
        }

        /// <summary>
        /// Delete Customer Type
        /// </summary>
        /// <param name="id"></param>
        /// <param name="confirmButton"></param>
        /// <returns></returns>
        [HttpPost, Authorize]
        public ActionResult Delete(int id, string confirmButton)
        {
            CustomerType customerType = _customerTypeRepository.GetCustomerTypeById(id);

            if (customerType != null)
            {
                _customerTypeRepository.Delete(customerType);
                _customerTypeRepository.Save();
                return View("Deleted");
            }
            return View("NotFound");
        }

        /// <summary>
        /// Handle Unknown Action
        /// </summary>
        /// <param name="actionName"></param>
        /// <exception cref="HttpException"></exception>
        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action not found");
        }

        #endregion
    }
}